<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Common\Model;

use Think\Model;

/**
 * 美容师Model模型类
 */
class BeauticianModel extends Model{
	
	protected $_validate = array(
		array('name','require','美容师姓名必须填写！'),
		array('profile_img','require','个人头像必须填写！'),
		array('mobile','require', '手机号必须填写！'),
		array('password','require','登录密码必须填写！'),
		array('beautician_desc','require', '美容师简介必须填写！'),
		array('mobile','require','手机不能为空！'),
		array('mobile','11','手机必须是11位数',1,'length'),
// 		array('password','require','密码不能为空！'),
// 		array('password','6,15','密码要在6-15位',1,'length'),
// 		array('reset_password','require','新密码不能为空！'),
	);
		
	/**
	 * 添加美容师
	 * 
	 * @param array $data
	 * @return boolean
	 */
	public function addBeautician($data) {
		$this->startTrans();
		$autoModel = D('AutoId');
		$id = $autoModel->createAutoId(array('type' => 'beautician'));	 
		$time = time();		
		$addData ['id'] = $id;
		$addData ['name'] = $data ['name'];
		$addData ['profile_img'] = $data ['profile_img'];
		$addData ['mobile'] = $data ['mobile'];
		$addData ['password'] = hashPassword($data ['password']);
		$addData ['address'] = $data ['address'];
		$addData ['province'] = $data ['province'];
		$addData ['city'] = $data ['city'];
		$addData ['longitude'] = $data ['longitude'];
		$addData ['latitude'] = $data ['latitude'];
		$addData ['beautician_desc'] = $data ['beautician_desc'];
		$addData ['search_area'] = $data ['search_area'];
		$addData ['created'] = $time;
		
		if (!$this->create($addData, 1)) {
			$this->rollback();
			return false;
		} else {
			if (!$this->add()) {
				$this->error = '添加美容师失败！'.$this->getError();
				$this->rollback();
				return false;
			}
		}
		
		$photos = I('post.certification_img_name');
		$upload_number = I('post.number');
		
		if ($photos) {
			$PhotoModel = D('Photo');
			$sort = 0;
			foreach ($photos as $key => $photo) {
				++$sort;
				$photoData = array(
					'target_id' => $id,
					'type' => 'certification',
					'img_name' => $photo,
					'sort' => $sort,
					'upload_number' => $upload_number,
					'created' => $time
				);
				$addResult = $PhotoModel->add($photoData);
				if (!$addResult) {
					$this->rollback();
					$this->error = '添加证书失败';
					return false;
				}
			}
		}
		
		$work_starts = I ('post.work_start');
		$work_ends = I ('post.work_end');
		$work_beauty_parlors = I ('post.work_beauty_parlor');
		$job_title_ids = I ('post.job_title_id');
		
		$experiences = array();
		if ($work_starts) {
			$ExperienceModel = D('WorkExperience');
			foreach ($work_starts as $key => $start) {
				$experience = array(
					'target_id' => $id,
					'start' => $start,
					'end' => $work_ends [$key],
					'beauty_parlor_name' => $work_beauty_parlors [$key],
					'job_title_id' => $job_title_ids [$key],
					'created' => $time
				);
				
				$addExpResult = $ExperienceModel->add($experience);
				if (!$addExpResult) {
					$this->rollback();
					$this->error = '添加工作经历失败';
				}
			}
		}
		
		$this->commit();
		return true;
	}
	
	/**
	 * 删除美容师缓存
	 * 
	 * @param number $id
	 */
	public function clearCache($id) {
		$cacheKey = $this->getCacheKey($id);
		S($cacheKey, null);
	}
	
	/**
	 * 检测美容师是否为美容院的员工
	 * 
	 * @param number $beautyId		美容院编号
	 * @param number $beauticianId	美容师编号
	 * @return array|boolean
	 */
	public function checkBeauticianIsEmploye($beautyId, $beauticianId) {
		return M('beauty_parlor_employe')
		->field(array('b.*'))
		->join('emp left join '.C('DB_PREFIX').'beautician as b on emp.beautician_id=b.id')
		->where(array('emp.bp_id' => $beautyId, 'emp.beautician_id' => $beauticianId, 'emp.status' => 1))
		->find();
		
// 		return $this->field(array('b.*'))
// 		->join('b left join '.C('DB_PREFIX').'beauty_parlor_employe as emp on b.id=emp.beautician_id')
// 		->where(array('emp.bp_id' => $beautyId, 'emp.beautician_id' => $beauticianId))->find();
	}
	
	/**
	 * 读取服务信息
	 * 
	 * @param number $id	美容师编号
	 * @return array|boolean
	 */
	public function getBeauticianInfo($id) {
// 		$cacheKey = $this->getCacheKey($id);
// 		$info = S($cacheKey);
// 		if (!$info) {
			$info = $this->where(array('id' => $id))->find();
// 			S($cacheKey, $info);
// 		}
		return $info;
	}
	
	/**
	 * 根据美容院推荐美容师
	 * 
	 * @param number $beautyId	美容院编号
	 * @return array|boolean
	 */
	public function getDefautlBeuatician($beautyId) {
		return M('beauty_parlor_employe')
		->field(array('b.*'))
		->join('emp left join '.C('DB_PREFIX').'beautician as b on emp.beautician_id=b.id')
		->where(array('emp.bp_id' => $beautyId, 'emp.status' => 1, 'b.status' => 1))
		->order('b.service_total DESC')
		->limit(1)
		->find();
	}
	
	/**
	 * 获取美容院的美容师
	 * 
	 * @param number $beautyId	美容院编号
	 * @return array|boolean
	 */
	public function getBeautyBeautician($beautyId) {
		return M('beauty_parlor_employe')
		->field(array('b.*'))
		->join('emp left join '.C('DB_PREFIX').'beautician as b on emp.beautician_id=b.id')
		->where(array('emp.bp_id' => $beautyId, 'emp.status' => 1, 'b.status' => 1))
		->order('b.service_total DESC')
		->select();
	}
	
	/**
	 * 缓存key
	 * 
	 * @param number $id	美容师编号
	 * @return string
	 */
	public function getCacheKey($id) {
		return 'Beautician_Info_'.$id;
	}
	
	/**
	 * 根据手机号码查询美容师
	 * @param string $mobile	手机号
	 * @return boolean|array
	 */
	public function findByMobile($mobile) {
		return $this->where(array('mobile' => $mobile))->find();
	}
	
	/**
	 * 美容师登录
	 * 
	 * @param string $mobile	手机号
	 * @param string $password	登录密码
	 * @return boolean|array
	 */
	public function login($mobile, $password) {
		$userObject = $userObject = $this->findByMobile($mobile);
		if (!$userObject) {
			return false;
		}
		
		$verifyResult = checkPassword($password, $userObject ['password']);
		if (!$verifyResult) {
			return false;
		}
		
		return $userObject;
	}
	
	/**
	 * 保存编辑
	 * 
	 * @param array $data
	 * @return boolean
	 */
	public function updateInfo($data) {
		$this->startTrans();
		$id = $data ['id'];
		
		$updateData ['id'] = $id;
		$updateData ['name'] = $data ['name'];
		$updateData ['profile_img'] = $data ['profile_img'];
		$updateData ['mobile'] = $data ['mobile'];
		$updateData ['province'] = $data ['province'];
		$updateData ['city'] = $data ['city'];
		$updateData ['address'] = $data ['address'];
		$updateData ['longitude'] = $data ['longitude'];
		$updateData ['latitude'] = $data ['latitude'];
		$updateData ['search_area'] = $data ['search_area'];
		
		if ($data ['password']) {
			$updateData ['password'] = hashPassword($data ['password']);
		}
		dump($updateData);die;
		$updateData ['beautician_desc'] = $data ['beautician_desc'];
		$item = $this->getBeauticianInfo($id);
		
		unset($item ['created']);
		unset($item ['status']);
		
		$hasModify = array();
		foreach ($updateData as $key => $value) {
			if ($item [$key] != $value) {
				$hasModify [$key] = $value;
			}
		}
		
		if ($hasModify && !$this->where(array('id' => $id))->save($updateData)) {
			$this->error = '编辑美容师失败！';
			$this->rollback();
			return false;
		}
		
		$PhotoModel = D('Photo');
		$photos = I('post.certification_img_name');
		$number = I('post.number');
		
		$PhotoModel->type = 'certification';
		$editResult = $PhotoModel->editTargetPhotos($id, $photos, $number);
		if (!$editResult) {
			$this->error = '编辑美容师证书失败！';
			$this->rollback();
			return false;
		}
		
		$exp_ids = I ('post.exp_id');
		$work_starts = I ('post.work_start');
		$work_ends = I ('post.work_end');
		$work_beauty_parlors = I ('post.work_beauty_parlor');
		$job_title_ids = I ('post.job_title_id');
		
		$experiences = array();
		if ($work_starts) {
			foreach ($work_starts as $key => $start) {
				$experiences [] = array(
					'start' => $start,
					'end' => $work_ends [$key],
					'beauty_parlor_name' => $work_beauty_parlors [$key],
					'job_title_id' => $job_title_ids [$key],
				);
				if (isset($exp_ids [$key]) && $exp_ids [$key]) {
					$experiences [$key] ['id'] = $exp_ids [$key];
				}
			}
		}
		
		if ($experiences) {
			$ExperienceModel = D('WorkExperience');
			$editResult = $ExperienceModel->editTargetExperiences($id, $experiences);
			if (!$editResult) {
				$this->error = '编辑美容师工作经历失败！';
				$this->rollback();
				return false;
			}
		}
		
		$cacheKey = $this->getCacheKey($id);
		S($cacheKey, NULL);
		$this->commit();
		return true;
	}
	
	/**
	 * 修改用户密码
	 * 
	 * @param string $mobile	手机号
	 * @param string $password	新密码
	 * @return boolean
	 */
	public function updatePassword($mobile, $password) {
		return $this->where(array('mobile' => $mobile))->save(array('password' => $password));
	}
	
	/**
	 * 更新美容师的评分
	 * 
	 * @param number $id	美容师编号
	 * @param array $data	评论得分数据
	 * @return boolean|array
	 */
	public function updateScore($id, $data) {
		$result = $this->where(array('id' => $id))->save($data);
		if (!$result) {
			return false;
		}
		
		$this->clearCache($id);
		return true;
	}
}